﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Xml.Linq;
using System.Linq;
using System.Globalization;
using System.ComponentModel;
using AllTalk.Data;

namespace AllTalk
{
    internal class Jid : INotifyPropertyChanged
    {
        private Presence _presence;
        public EventHandler<PresenceChangedEventArgs> PresenceChanged { get; set; }

        /*megha@gmail.com/something*/
        public string UserName { get; private set; } /*megha*/
        public string Domain { get; private set; } /*gmail*/
        public string UserNameDomain { get; private set; }
        public string ResourceName { get; private set; } /*something*/
        public string Value { get; private set; }
        
        public Presence Presence
        {
            get
            {
                return _presence;
            }
            private set
            {
                if (_presence != value)
                {
                    Presence originalValue = _presence;
                    _presence = value;
                    NotifyPropertyChanged("Presence");
                    if (PresenceChanged != null)
                    {
                        PresenceChanged(this, new PresenceChangedEventArgs(originalValue, _presence));
                    }
                }
            }
        }
        public int Priority { get; private set; }

        public Jid(string jid)
        {
            this.Value = jid;

            int ampersandPosition = jid.IndexOf('@');
            this.UserName = jid.Substring(0, ampersandPosition);

            int slashPosition = jid.IndexOf('/', ampersandPosition);
            if (slashPosition < 0)
            {
                slashPosition = jid.Length;
            }
            this.Domain = jid.Substring(ampersandPosition + 1, slashPosition - ampersandPosition - 1);
            this.UserNameDomain = jid.Substring(0, slashPosition);

            if (slashPosition != jid.Length)
            {
                this.ResourceName = jid.Substring(slashPosition + 1, jid.Length - slashPosition - 1);
            }
        }

        internal void UpdatePresence(XElement presenceXml)
        {
            string presenceString = presenceXml.GetAttribute("type");

            XElement showElement = presenceXml.Descendants("show").FirstOrDefault();
            if (showElement != null)
            {
                presenceString = showElement.Value;
            }

            if (!String.IsNullOrEmpty(presenceString))
            {
                this.Presence = (Presence)Enum.Parse(typeof(Presence), presenceString, /*ignoreCase*/ true);
            }
            else
            {
                this.Presence = Presence.Chat;
            }

            XElement priorityElement = presenceXml.Descendants("priority").FirstOrDefault();
            int priority = 0;
            if (priorityElement != null)
            {
                priority = Convert.ToInt32(priorityElement.Value);
                this.Priority = priority;
            }
        }

        public override string ToString()
        {
            return this.Value;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
